package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author stone
 * @date 2021/11/23 15:08
 */
@RestController
public class ArticleController {

    //@PathVariable注解的value属性值要和占位符的值相同，形参名不用
    @RequestMapping("{username}/article/details/{id}")
    public BaseRespVo article(@PathVariable("username") String name,
                              @PathVariable("id") Integer articleId){
        return BaseRespVo.ok("username:" + name + ";id=" + articleId);
    }

    //根据articleId做删除
    //localhost:8080/article/remove?id=5
    @RequestMapping("article/remove")
    public BaseRespVo removeArticle(@RequestParam("id") Integer id) {
        return BaseRespVo.ok(id);
    }

    //使用字符串接收对应请求头的值,也可以使用字符串数组来接收
    //如果使用数组来接收，就是将这个值转换为数组，根据逗号做的分隔
    @RequestMapping("header")
    public BaseRespVo header(@RequestHeader("Accept")String[] accept,
                             @RequestHeader("Host")String host) {
        return BaseRespVo.ok(null);
    }

    @RequestMapping("cookie")
    public BaseRespVo cookie(@CookieValue("xuejia")String value) {
        return BaseRespVo.ok(value);
    }

    //localhost:8080/session1?username=songge&password=ligenli
    @RequestMapping("session1")
    public BaseRespVo session1(HttpSession session, User user) {
        session.setAttribute("user",user);
        return BaseRespVo.ok(user);
    }
    //向session中放入的是什么类型的，最终取出的也是该类型的
    @RequestMapping("session2")
    public BaseRespVo session2(@SessionAttribute("user")User user) {
        return BaseRespVo.ok(user);
    }
}
